package com.app.couponsharelife.ui.adapter

import androidx.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.PageTipBean
import com.app.couponsharelife.data.bean.UserTeamMemberBean
import com.app.couponsharelife.databinding.TeamListItemBinding
import com.app.couponsharelife.ui.listener.OnClickListener

/**
 * Created by guluwa on 2018/11/23.
 */
class TeamListAdapter(val list: MutableList<Any>,
                      private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    fun setData(data: List<Any>, page: Int) {
        if (page == 2) list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return when (viewType) {
            0 -> ViewHolderTip(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.page_list_tip_item, parent, false))
            else -> ViewHolder(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.team_list_item, parent, false))
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (list[position] is PageTipBean) 0 else 1
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        if (getItemViewType(position) == 0) {
            (holder as ViewHolderTip).databinding.pageTipBean = list[position] as PageTipBean
        } else {
            val recordsBean = list[position] as UserTeamMemberBean.RecordsBean
            Glide.with((holder as ViewHolder).itemView.context).asBitmap()
                    .apply(RequestOptions().circleCrop()
                            .placeholder(R.drawable.ic_tribe_boy_thumb)
                            .error(R.drawable.ic_tribe_boy_thumb))
                    .load(recordsBean.headimgurl)
                    .into(holder.databinding.ivUserImage)
            holder.databinding.tvUserName.text = recordsBean.nickName
            holder.databinding.tvUserPhoneNum.text = recordsBean.mobile
            holder.databinding.tvUserGrade.text =recordsBean.grade //if (recordsBean.grade == "VIP") "会员" else "超级会员"
            if (recordsBean.createTime.length > 10)
                holder.databinding.tvUserRegisterDate.text = recordsBean.createTime.substring(0, 10)
            else
                holder.databinding.tvUserRegisterDate.text = recordsBean.createTime
        }
    }

    inner class ViewHolder(val databinding: TeamListItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root)
}